package com.concurrent.chapter1.concurrent04;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
//
public class Service {
	private Semaphore semaphore = new Semaphore(3);
	public void testMethod() {
		try {
			if (semaphore.tryAcquire(3, 3, TimeUnit.SECONDS)) {//尝试在3秒内获得3个许可
				System.out.println(Thread.currentThread().getName() + "首选进入");
				for (int i = 0; i < Integer.MAX_VALUE; i++) {
					String newString = new String();
					Math.random();
				}
				semaphore.release(3);
			} else {
				System.out.println(Thread.currentThread().getName() + "未成功进入");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
